#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mypushbutton.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    ,LineType(1)
{
    ui->setupUi(this);

    myPushbutton * pushbutton = new  myPushbutton(ui->centralwidget);
    pushbutton->setGeometry(QRect(200,255,80,30));
    pushbutton->setText("登录");
    pushbutton->setShortcut(QKeySequence("E"));

    myPushbutton * pushbutton1 = new  myPushbutton(ui->centralwidget);
    pushbutton1->setGeometry(QRect(400,255,80,30));
    pushbutton1->setText("退出");
    pushbutton1->setShortcut(QKeySequence("N"));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);//定义一个画家

    QBrush brush(QColor(123,196,111));//定义一个刷子
    painter.setBrush(brush);//把刷子给画家

    QPen pen;  // creates a default pen
    pen.setWidth(2);//设置笔的宽度为2
    pen.setColor(Qt::blue);//把颜色赋值给笔
    painter.setPen(pen);

    //    QLineF line(10.0, 80.0, 90.0, 20.0);//定义一根线
    //    painter.drawLine(10.0, 80.0, 90.0, 20.0);

    if(LineType == 1)
    {
        painter.drawLine(start,end); //画鼠标正在绘制的那条线
        
    }
    else if(LineType == 2)
    {

        for (int var = 0; var < Pointlist.size() -1 ; ++var)
        {
            painter.drawLine(Pointlist[var] , Pointlist[var+1]);
        }
    }

    for (int var = 0; var < linelist.size(); ++var)
    {
        painter.drawLine(linelist[var]);
    }

    for (int var = 0; var < LinePointlist.size(); ++var)
    {
        for(int i = 0 ; i < LinePointlist[var].size() - 1;i++)
        {
            painter.drawLine(LinePointlist[var][i],LinePointlist[var][i+1]);
        }
    }

}

void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    qDebug()<<"鼠标在窗口上按下并移动，画线的终点位置是："<<e->pos()<<endl;
    if(LineType == 1)
    {
        end = e->pos();
    }
    else if(LineType == 2)
    {
        Pointlist.push_back(e->pos());
    }
    repaint();
}

void MainWindow::mousePressEvent(QMouseEvent *e)
{
    qDebug()<<"鼠标按下，画线的起始位置为："<<e->pos()<<endl;
    if(LineType == 1)
    {
        start = e->pos();
    }
    else if(LineType == 2)
    {
        Pointlist.push_back(e->pos());
    }
    repaint();
}

void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
    if(LineType == 1)
    {
        Q_UNUSED(e);
        QLine line(start,end);//把鼠标按下时的点和鼠标移动时的点在鼠标放开的时候构建成一条线
        linelist.push_back(line);//把线条的两个点存放到链表
        repaint();
    }
    else if(LineType == 2)
    {
        LinePointlist.push_back(Pointlist);
        Pointlist.clear();
    }

    
}



void MainWindow::on_radioButton_clicked()
{
    qDebug()<<1<<endl;
    LineType = 1;

}

void MainWindow::on_radioButton_2_clicked()
{
    qDebug()<<2<<endl;
    LineType = 2;
}
